package conversationGroup

import (
	"common/dao/model"
	"context"
	"errors"
	"gorm.io/gorm"
	"time"

	"gateway/internal/svc"
	"gateway/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateConversationGroupLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateConversationGroupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateConversationGroupLogic {
	return &UpdateConversationGroupLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateConversationGroupLogic) UpdateConversationGroup(req *types.UpdateConversationGroup) error {
	// todo: add your logic here and delete this line
	var (
		db   = l.svcCtx.DB.Table(model.TableNameConversationGroup)
		data = model.ConversationGroup{}
	)
	err := db.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("id = ?", req.ID).Find(&data).Error; err != nil {
			return err
		}
		if data.ID == 0 {
			return errors.New("未查询到数据")
		}
		data.Title = req.Title
		data.UpdateTime = time.Now().Unix()
		if err := tx.Save(&data).Error; err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return err
	}
	return nil
}
